#include "GameWidget.h"
#include "ShopWidget.h"

#include <QtGui>

void GameWidget::setField( Field* f ) {
    field = f;
    shop = new ShopWidget( this, field->getShop() );
    connect( timer, SIGNAL( timeout() ),
             shop, SLOT( updateLabel() ) );
}

void GameWidget::pause() {
    if( timer->isActive() ) {
        timer->stop();
    } else {
        timer->start( INTERVAL );
    }
}

void GameWidget::keyPressEvent( QKeyEvent* event ) {
    switch( event->key() ) {
        case( Qt::Key_R ):
            pause();
            break;
        case( Qt::Key_Space ):
            field->getGraph()->saveScreen();
            break;
        case( Qt::Key_Tab ):
            field->getGraph()->setHalluMode(
                               !field->getGraph()->getHalluMode() );
            if( !timer->isActive() ) field->getGraph()->paintAll();
            break;
        case( ShopWidget::SHOPKEY ):
            shop->open();
            if( timer->isActive() ) pause();
            break;
        case( Qt::Key_Minus ):
            field->getGraph()->zoomOut( 2 );
            if( !timer->isActive() ) field->getGraph()->paintAll();
            break;
        case( Qt::Key_Plus ):
            field->getGraph()->zoomIn( 2 );
            if( !timer->isActive() ) field->getGraph()->paintAll();
            break;
        case( Qt::Key_T ) :
            field->saveInFile( "save.txt", "saveConfig.txt" );
            if( !timer->isActive() ) field->getGraph()->paintAll();
            break;
        case( Qt::Key_G ):
            field->loadFromFile( "save.txt", "saveConfig.txt" );
            if( !timer->isActive() ) field->getGraph()->paintAll();
            break;
        case( Qt::Key_B ):
            field->getGraph()->setZoomToPlayer(
                        !(field->getGraph()->getZoomToPlayer()) );
            if( !timer->isActive() ) field->getGraph()->paintAll();
            break;
        case( Qt::Key_Up ):
            field->getGraph()->moveZoomCenterUp( 1 );
            if( !timer->isActive() ) field->getGraph()->paintAll();
            break;
        case( Qt::Key_Right ):
            field->getGraph()->moveZoomCenterRight( 1 );
            if( !timer->isActive() ) field->getGraph()->paintAll();
            break;
        case( Qt::Key_Left ):
            field->getGraph()->moveZoomCenterLeft( 1 );
            if( !timer->isActive() ) field->getGraph()->paintAll();
            break;
        case( Qt::Key_Down ):
            field->getGraph()->moveZoomCenterDown( 1 );
            if( !timer->isActive() ) field->getGraph()->paintAll();
            break;
        case( Qt::Key_Backspace ):
            field->clear();
            break;
        default:
            if( timer->isActive() ) {
                emit keyPress( event );
            }
            break;
    }
}

void GameWidget::wheelEvent( QWheelEvent * event ) {
    if( event == 0 ) return;
    else{
        int numberOfSteps = event->delta() / 120;
        if( numberOfSteps > 0 ) {
            field->getGraph()->zoomIn( numberOfSteps );
            if( !timer->isActive() ) field->getGraph()->paintAll();
        } else if ( numberOfSteps < 0 ) {
            field->getGraph()->zoomOut( -numberOfSteps );
            if( !timer->isActive() ) field->getGraph()->paintAll();
        }
    }
}
